import 'package:inmotion_pro/pages/others/common_enum/vehicle_owner_type.dart';
import 'package:inmotion_pro/utils/date_format_util.dart';
import 'package:safemap/safemap.dart';

/// 验证码发送平台
enum CodePlatform { phone, email }

/// 安全操作类型
enum SecurityType { resetPwd, updatePhone, updateEmail }

/// 获取验证码业务类型
/// register: 注册
/// login: 登录
/// resetPwd: 重置密码
/// security: 安全操作
/// bind: 绑定手机号/邮箱
enum CodeType {
  register,
  login,
  resetPwd,
  security,
  bind;

  String get val {
    String type = '';
    switch (this) {
      case CodeType.register:
        type = 'register';
        break;
      case CodeType.login:
        type = 'login';
        break;
      case CodeType.resetPwd:
        type = 'reset-pwd';
        break;
      case CodeType.security:
        type = 'security';
        break;
      case CodeType.bind:
        type = 'bind';
        break;
      default:
        type = 'login';
    }
    return type;
  }
}

// 用户模型
class UserModel {
  int uid = 0;
  String token = "";
  String xAuth = "";
  int timezoneOffset = 0;
  String signature = "";
  int coin = 0;
  String country = "";
  String province = "";
  String city = "";
  String name = "";
  String nickname = "";
  String birthday = "";
  String avatar = "";
  String email = "";
  String phone = "";
  int checkinContinuedDays = 0;
  int sex = 0;
  bool isSelected = false;
  // 主车主/副车主
  VehicleOwnerType ownerType = VehicleOwnerType.none;
  // 注册时间
  String createdAt = "";

  // 常用的Map转换构造函数
  UserModel.fromMap(Map<String, dynamic> map) {
    SafeMap safeMap = SafeMap(map);
    uid = safeMap['id'].toInt ??
        safeMap['uid'].toInt ??
        safeMap['user_id'].toInt ??
        0;
    token = safeMap['token'].string ?? "";
    xAuth = safeMap['x_auth'].string ?? "";
    timezoneOffset = safeMap['timezone_offset'].toInt ?? 0;
    signature = safeMap['signature'].string ?? "";
    coin = safeMap['coin'].toInt ?? 0;
    country = safeMap['country'].string ?? "";
    province = safeMap['province'].string ?? "";
    city = safeMap['city'].string ?? "";
    name = safeMap['name'].string ?? "";
    nickname = safeMap['nickname'].string ?? "";
    birthday = safeMap['birthday'].string ?? "";
    avatar = safeMap['avatar'].string ?? "";
    email = safeMap['email'].string ?? "";
    phone = safeMap['phone'].string ?? "";
    checkinContinuedDays = safeMap['checkin_continued_days'].toInt ?? 0;
    sex = safeMap['sex'].toInt ?? 0;
    isSelected = safeMap['is_selected'].toInt == 1 ? true : false;
    ownerType = VehicleOwnerType.values.firstWhere(
        (e) => e.val == (safeMap['type'].toInt ?? 0),
        orElse: () => VehicleOwnerType.none);
    if (safeMap['created_at'].string != null) {
      int? timestamp =
          DateFormatUtil.parseDateString(safeMap['created_at'].string!)
              ?.millisecondsSinceEpoch;
      createdAt =
          timestamp != null ? DateFormatUtil.formatTimestamp(timestamp) : "";
    }
  }

  Map<String, dynamic> toMap() {
    return {
      'id': uid,
      'token': token,
      'x_auth': xAuth,
      'timezone_offset': timezoneOffset,
      'signature': signature,
      'coin': coin,
      'country': country,
      'province': province,
      'city': city,
      'name': name,
      'nickname': nickname,
      'birthday': birthday,
      'avatar': avatar,
      'email': email,
      'phone': phone,
      'checkin_continued_days': checkinContinuedDays,
      'sex': sex,
      'is_selected': isSelected ? 1 : 0,
      'type': ownerType.val,
      'created_at': createdAt,
    };
  }
}
